<?php
/**
 * e-CMS
 * Copyright (c)	2008, mdnguyen86@yahoo.com
 *
 */
class Categories
{
  var $root_tree=1;
  function admin_index($root=1){
  	if($this->params['named']['root'])
		$root= $this->params['named']['root'];
  	$this->layout = 'admin';
  	$rows = $this->Category->findAll("pid=".$root);
	$this->set('rows',$rows);
	$this->set('root',$root);
	$this->set('root_tree',$this->root_tree);
	$this->set('title_bar','Quản trị thể loại');
  }
  function admin_add(){
  	$this->layout = 'admin';
	$this->set('title_bar','Quản trị thể loại >> Thêm');
	$filter_cat = $this->admin_tree_filter();
	$cats = $this->admin_list_cat(1);
	$this->set('filter_cat',$filter_cat);
	$this->set('cats',$cats);
  }
  function admin_tree_filter_cat($root=1,$selected=0,$comp="a",$name="cat",$onchange="TreeFilterChanged",$where=" (comp <> 7 and comp <> 14)"){  	
  	$level = 1;
	$filter_cat = '<select class="styledselect-categories" name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'(this.value);">';
	$filter_cat .= "\n<option value='".$root."'>Chọn Thể Loại</option>\n";	
	$this->dequicat($root,$filter_cat,$level,$selected,$comp,$where);
	$filter_cat .= '</select>';
	return $filter_cat;
  }
  function dequicat($root,&$html,$level,$selected,$comp,$where){
	global $db;
	$sql = "select id,name_vn,pid,comp from categories where pid=".$root." and ".$where." order by num asc, id asc";
	//print $sql. "</br>";
	$all = $db->getAll($sql);
	if($all){
		for($i=0;$i<count($all);$i++){
			$temp = "";
			for($j=0;$j<$level;$j++)
				$temp .="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			$checked = "";
			if($selected == $all[$i]['id'])
				$checked = " selected ";
			$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
			if($all[$i]['comp'] == $comp)
				$html .= "color:#0000ff;";
			if($all[$i]['pid']==1)
				$html .= "font-weight:bold;";
			$html .= "\">".$temp."|-> ".$all[$i]['name_vn']."</option>\n";
			$this->dequicat($all[$i]['id'],$html,$level+1,$selected,$comp,$where);
		}
	}
  }  
  function admin_tree_filter_change($root=1,$selected=0,$comp="a",$type, $name="cat",$onchange="ChangeFindCidList",$where="1"){  	
  global $table_edit;
  	$level = 1;
	$filter_cat = '<select name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'(this.value, '."'$table_edit'".');">';
	
	if($type == 'product')
	{
		$this->dequisanpham($root,$filter_cat,$level,$selected,$comp,$where);	
	}
	else if($type == 'article')
	{
		$this->dequitintuc($root,$filter_cat,$level,$selected,$comp,$where);	
	}
	else if($type == 'images')
	{
		$this->dequiimages($root,$filter_cat,$level,$selected,$comp,$where);	
	}
	else if($type == 'layout')
	{
		$this->dequilayout($root,$filter_cat,$level,$selected,$comp,$where);	
	}
	
	$filter_cat .= '</select>';
	
	return $filter_cat;
  }
  function admin_tree_filter($root=1,$selected=0,$comp="a",$type, $name="cat",$onchange="TreeFilterChanged",$where="1"){  	
  global $table_edit;
  	$level = 1;
	$filter_cat = '<select name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'(this.value);">';
	
	if($type == 'product')
	{
		$this->dequisanpham($root,$filter_cat,$level,$selected,$comp,$where);	
	}
	else if($type == 'article')
	{
		$this->dequitintuc($root,$filter_cat,$level,$selected,$comp,$where);	
	}
	else if($type == 'images')
	{
		$this->dequiimages($root,$filter_cat,$level,$selected,$comp,$where);	
	}
	else if($type == 'layout')
	{
		$this->dequilayout($root,$filter_cat,$level,$selected,$comp,$where);	
	}
	
	$filter_cat .= '</select>';
	
	return $filter_cat;
  }
  function admin_tree_filter2($root=1,$selected=0,$comp="a",$name="cat",$onchange="TreeFilterChanged",$where="1"){  	
  	$level = 1;
	$filter_cat = '<select class="styledselect-categories" name="'.$name.'" id="'.$name.'" onchange="'.$onchange.'(this.value);">';
	$filter_cat .= "\n<option value='".$root."'>Top</option>\n";	
	$this->dequi($root,$filter_cat,$level,$selected,$comp,$where);
	$filter_cat .= '</select>';
	return $filter_cat;
  }
  function admin_list_cat($root=1){
	$cats = array();
	$this->dequi2($root,$cats);
	return $cats;
  }
  function admin_save_order(){
  
	$this->redirect('/'.$this->data['url']);
  }
  function dequi($root,&$html,$level,$selected,$comp,$where){
	global $db;
	$sql = "select id,name_vn,pid,comp from categories where pid=".$root." and ".$where." order by num asc, id asc";
	//echo $sql;
	$all = $db->getAll($sql);
	//print_r($all);
	//print "<br/>";
	if($all){
		for($i=0;$i<count($all);$i++){
			$temp = "";
			for($j=0;$j<$level;$j++)
				$temp .="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			$checked = "";
			if($selected == $all[$i]['id'])
				$checked = " selected ";
			$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
			if($all[$i]['comp'] == $comp)
				$html .= "color:#0000ff;";
			if($all[$i]['pid']==1)
				$html .= "font-weight:bold;";
			$html .= "\">".$temp."|-> ".$all[$i]['name_vn']."</option>\n";
			$this->dequisanpham($all[$i]['id'],$html,$level+1,$selected,$comp,$where);
		}
	}
  } 
  function dequisanpham($root,&$html,$level,$selected,$comp,$where){
	global $db;
	$sql = "select id,name_vn,pid,comp from categories where pid=".$root." and (comp = '2' or comp = '0' or comp = '') and ".$where." order by num asc, id asc";
	//echo $sql;
	$all = $db->getAll($sql);
	//print_r($all);
	//print "<br/>";
	if($all){
		for($i=0;$i<count($all);$i++){
			$temp = "";
			for($j=0;$j<$level;$j++)
				$temp .="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			$checked = "";
			if($selected == $all[$i]['id'])
				$checked = " selected ";
			if($all[$i]['id']=='121')
			{	
				$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
				if($all[$i]['comp'] == $comp)
					$html .= "color:#0000ff;";
				if($all[$i]['pid']==1)
					$html .= "font-weight:bold;";
				$html .= "\">".$temp."Chọn Sản Phẩm"."</option>\n";
			}
			else
			{
				$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
				if($all[$i]['comp'] == $comp)
					$html .= "color:#0000ff;";
				if($all[$i]['pid']==1)
					$html .= "font-weight:bold;";
				$html .= "\">".$temp."|--> ".$all[$i]['name_vn']."</option>\n";
			}
			$this->dequisanpham($all[$i]['id'],$html,$level+1,$selected,$comp,$where);
		}
	}
  }  
  function dequitintuc($root,&$html,$level,$selected,$comp,$where){
	global $db;
	$sql = "select id,name_vn,pid,comp from categories where pid=".$root." and (comp = '1' or comp = '0' or comp = '') and ".$where." order by num asc, id asc";
	//echo $sql;
	$all = $db->getAll($sql);
	//print_r($all);
	//print "<br/>";
	if($all){
		for($i=0;$i<count($all);$i++){
			$temp = "";
			for($j=0;$j<$level;$j++)
				$temp .="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			$checked = "";
			if($selected == $all[$i]['id'])
				$checked = " selected ";
			if($all[$i]['id']=='121')
			{	
				$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
				if($all[$i]['comp'] == $comp)
					$html .= "color:#0000ff;";
				if($all[$i]['pid']==1)
					$html .= "font-weight:bold;";
				$html .= "\">".$temp."Chọn Tin Tức"."</option>\n";
			}
			else
			{
				$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
				if($all[$i]['comp'] == $comp)
					$html .= "color:#0000ff;";
				if($all[$i]['pid']==1)
					$html .= "font-weight:bold;";
				$html .= "\">".$temp."|--> ".$all[$i]['name_vn']."</option>\n";
			}
			$this->dequitintuc($all[$i]['id'],$html,$level+1,$selected,$comp,$where);
		}
	}
  }  
  function dequiimages($root,&$html,$level,$selected,$comp,$where){
	global $db;
	$sql = "select id,name_vn,pid,comp from categories where pid=".$root." and (comp = '9' or comp = '0' or comp = '') and ".$where." order by num asc, id asc";
	//echo $sql;
	$all = $db->getAll($sql);
	//print_r($all);
	//print "<br/>";
	if($all){
		for($i=0;$i<count($all);$i++){
			$temp = "";
			for($j=0;$j<$level;$j++)
				$temp .="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			$checked = "";
			if($selected == $all[$i]['id'])
				$checked = " selected ";
			if($all[$i]['id']=='121')
			{	
				$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
				if($all[$i]['comp'] == $comp)
					$html .= "color:#0000ff;";
				if($all[$i]['pid']==1)
					$html .= "font-weight:bold;";
				$html .= "\">".$temp."Chọn Tin Tức"."</option>\n";
			}
			else
			{
				$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
				if($all[$i]['comp'] == $comp)
					$html .= "color:#0000ff;";
				if($all[$i]['pid']==1)
					$html .= "font-weight:bold;";
				$html .= "\">".$temp."|--> ".$all[$i]['name_vn']."</option>\n";
			}
			$this->dequiimages($all[$i]['id'],$html,$level+1,$selected,$comp,$where);
		}
	}
  } 
  function dequilayout($root,&$html,$level,$selected,$comp,$where){
	global $db;
	$sql = "select id,name_vn,pid,comp from categories where pid=".$root." and (comp = '28' or comp = '0' or comp = '') and ".$where." order by num asc, id asc";
	//echo $sql;
	$all = $db->getAll($sql);
	//print_r($all);
	//print "<br/>";
	if($all){
		for($i=0;$i<count($all);$i++){
			$temp = "";
			for($j=0;$j<$level;$j++)
				$temp .="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
			$checked = "";
			if($selected == $all[$i]['id'])
				$checked = " selected ";
			if($all[$i]['id']=='121')
			{	
				$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
				if($all[$i]['comp'] == $comp)
					$html .= "color:#0000ff;";
				if($all[$i]['pid']==1)
					$html .= "font-weight:bold;";
				$html .= "\">".$temp."Chọn Template"."</option>\n";
			}
			else
			{
				$html .= "<option value='".$all[$i]['id']."' ".$checked." style=\"";
				if($all[$i]['comp'] == $comp)
					$html .= "color:#0000ff;";
				if($all[$i]['pid']==1)
					$html .= "font-weight:bold;";
				$html .= "\">".$temp."|--> ".$all[$i]['name_vn']."</option>\n";
			}
			$this->dequilayout($all[$i]['id'],$html,$level+1,$selected,$comp,$where);
		}
	}
  }  
  function dequi2($root,&$cats){
	$all = $this->Category->findSelfParent($root);
	if($all){
		for($i=0;$i<count($all);$i++){			
			$cats[] = array('id'=>$all[$i]['c1']['id'],
							'name'=>$all[$i]['c1']['name'],
							'ordering'=>$all[$i]['c1']['ordering'],
							'pid'=>$all[$i]['c1']['pid'],
							'parent'=>$all[$i]['c2']['parent']);
		}
		for($i=0;$i<count($all);$i++){			
			$this->dequi2($all[$i]['c1']['id'],$cats);
		}
	}
  }
}
?>